package com.cloudweb.oa.controller;

import cn.js.fan.util.ParamUtil;
import cn.js.fan.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.oa.api.ISQLCtl;
import com.cloudweb.oa.cache.UserCache;
import com.cloudweb.oa.entity.DeptUser;
import com.cloudweb.oa.entity.Role;
import com.cloudweb.oa.entity.User;
import com.cloudweb.oa.security.AuthUtil;
import com.cloudweb.oa.service.HomeService;
import com.cloudweb.oa.service.MacroCtlService;
import com.cloudweb.oa.utils.Base64Util;
import com.cloudweb.oa.utils.SpringUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.android.Privilege;
import com.redmoon.oa.dept.DeptDb;
import com.redmoon.oa.dept.DeptMgr;
import com.redmoon.oa.dept.DeptView;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.flow.FormField;
import com.redmoon.oa.flow.Leaf;
import com.redmoon.oa.flow.WorkflowDb;
import com.redmoon.oa.stamp.StampDb;
import com.redmoon.oa.stamp.StampPriv;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@Controller
public class MobileMacroCtlController {

    @Autowired
    private HomeService homeService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    AuthUtil authUtil;

    @Autowired
    UserCache userCache;

    @ResponseBody
    @RequestMapping(value = "/public/android/macro/getSqlCtlOnChange", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public String getSqlCtlOnChange(HttpServletRequest request, @RequestParam(value = "", required = true)String skey) {
        JSONObject json = new JSONObject();
        Privilege pvg = new Privilege();
        boolean re = pvg.Auth(skey);
        if (re) {
            json.put("res", "-2");
            json.put("msg", "时间过期");
            return json.toString();
        }

        pvg.doLogin(request, skey);

        int flowId = ParamUtil.getInt(request, "flowId", -1);
        String fieldName = ParamUtil.get(request, "fieldName");
        String formCode = ParamUtil.get(request, "formCode");

        if ("".equals(formCode)) {
            WorkflowDb wf = new WorkflowDb();
            wf = wf.getWorkflowDb(flowId);

            Leaf lf = new Leaf();
            lf = lf.getLeaf(wf.getTypeCode());
            formCode = lf.getFormCode();
        }

        FormDb fd = new FormDb();
        fd = fd.getFormDb(formCode);

        FormField ff = fd.getFormField(fieldName);
        if (ff==null) {
            json.put("res", "-1");
            json.put("msg", "表单：" + fd.getName() + " formCode=" + formCode + " 字段： " + fieldName + " is null");
            return json.toString();
        }

        MacroCtlService macroCtlService = SpringUtil.getBean(MacroCtlService.class);
        ISQLCtl sqlCtl = macroCtlService.getSQLCtl();

        JSONObject field = null;
        try {
            field = sqlCtl.getCtl(request, flowId, ff);
        }
        catch (Exception e) {
            LogUtil.getLog(getClass()).error(e);
        }
        if (field!=null) {
            json.put("res", "1");
            json.put("msg", "操作成功！");
            json.put("field", field);
        }
        else {
            json.put("res", "-1");
            json.put("msg", "操作失败！");
        }
        return json.toString();
    }

    /**
     * 百度地图
     * @param model
     * @return
     */
    @RequestMapping(value = "/m/macro/baiduMapLocation")
    public String baiduMapLocation(Model model) {
        model.addAttribute("nav", homeService.getNav());
        String code = ParamUtil.get(request,"code");
        String val = ParamUtil.get(request,"val");
        String editable = ParamUtil.get(request, "editable");
        model.addAttribute("code", code);
        model.addAttribute("val", val);
        model.addAttribute("editable", editable);
        return "th/m/macro/baidu_map_location";
    }

    /**
     * 天地图
     * @param model
     * @return
     */
    @RequestMapping(value = "/m/macro/tdMapLocation")
    public String tdMapLocation(Model model) {
        model.addAttribute("nav", homeService.getNav());
        String code = ParamUtil.get(request,"code");
        String val = ParamUtil.get(request,"val");
        model.addAttribute("code", code);
        model.addAttribute("val", val);
        return "th/m/macro/td_map_location";
    }

    @RequestMapping(value = "/m/macro/signImgCtlWin")
    public String signImgCtlWin(Model model) {
        model.addAttribute("nav", homeService.getNav());

        String code = ParamUtil.get(request, "code");
        User user = userCache.getUser(authUtil.getUserName());
        String realName = user.getRealName();
        String pwd = user.getPwdRaw();

        StampPriv sp = new StampPriv();
        Map<String, String> map = new HashMap<>();
        List<StampDb> list = new ArrayList<>();
        Vector<StampDb> v = sp.getStampsOfUser(authUtil.getUserName());
        // 去重
        for (StampDb stampDb : v) {
            if (stampDb != null && stampDb.isLoaded()) {
                if (map.containsKey(String.valueOf(stampDb.getId()))) {
                    continue;
                }
                map.put(String.valueOf(stampDb.getId()), "");
                list.add(stampDb);
            }
        }
        model.addAttribute("stamps", list);

        String pwdRawStr = Base64Util.decode(pwd);
        model.addAttribute("realName", realName);
        model.addAttribute("pwd", pwdRawStr);
        model.addAttribute("code", code);

        return "th/m/macro/macro_ctl_sign_img_win";
    }

    @RequestMapping(value = "/m/macro/signWin")
    public String signWin(Model model) {
        model.addAttribute("nav", homeService.getNav());

        String code = ParamUtil.get(request,"code");
        User user = userCache.getUser(authUtil.getUserName());
        String realName = user.getRealName();
        String pwd = user.getPwdRaw();

        String pwdRawStr = Base64Util.decode(pwd);
        model.addAttribute("realName", realName);
        model.addAttribute("pwd", pwdRawStr);
        model.addAttribute("code", code);

        return "th/m/macro/macro_ctl_sign_win";
    }

    @RequestMapping(value = "/m/macro/deptWin")
    public String deptWin(Model model) {
        model.addAttribute("nav", homeService.getNav());

        com.redmoon.oa.pvg.Privilege privilege = new com.redmoon.oa.pvg.Privilege();
        String dirCode = "root";
        String formCode = ParamUtil.get(request, "formCode");
        DeptMgr dir = new DeptMgr();
        DeptDb leaf = dir.getDeptDb(dirCode);
        DeptView tv = new DeptView(request, leaf);
        String jsonData = "";
        try {
            jsonData = tv.getJsonString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("jsonData", jsonData);
        model.addAttribute("formCode", formCode);

        return "th/m/macro/macro_dept_win";
    }
}
